package org.mspring.mlog.web.task.comment;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.mspring.mlog.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @since 2014-04-10 14:57:55
 * @author Gao Youbo
 */
@Aspect
@Component
public class CommentNoticeAspect {

    @Autowired
    private CommentNoticeTask commentNoticeTask;
    @Autowired
    private CommentReplyNoticeTask commentReplyNoticeTask;

    @AfterReturning(value = "execution(* org.mspring.mlog.service.CommentService.create(..))", returning = "comment")
    public void noticeTask(JoinPoint joinPoint, Comment comment) {
        if (comment == null || !StringUtils.equals(comment.getStatus(), Comment.Status.APPROVED)) {
            return;
        }
        Map<Object, Object> context = new HashMap<>();
        context.put("comment", comment);
        try {
            // 评论回复
            if (comment != null && comment.getParent() != null) {
                commentReplyNoticeTask.doAsyncTask(context);
            }
            // 文章评论
            if (comment != null) {
                commentNoticeTask.doAsyncTask(context);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
